package diploma.kisel.modelas.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import diploma.kisel.hibernate.util.HibernateUtil;
import diploma.kisel.modelas.domain.Concept;
import diploma.kisel.modelas.domain.Database;
import diploma.kisel.modelas.repository.api.DatabaseRepository;

public class DatabaseRepositoryImpl implements DatabaseRepository{
	
	private EntityManager em = HibernateUtil.getEm();

	@Override
	public List<Database> findAll() {
		List<Database> databases= em.createNamedQuery(
				"Database.findAll", Database.class).getResultList();		
		return databases;		
	}

	@Override
	public Database findById(int id) {
		TypedQuery<Database> query = em.createNamedQuery("Database.findById", Database.class);
		query.setParameter("id", id);
		return query.getSingleResult();
	}

	@Override
	public Database save(Database database) {
		em.getTransaction().begin();
		em.persist(database);
		em.getTransaction().commit();
		return database;
	}

	@Override
	public Database update(Database database) {
		em.getTransaction().begin();
		em.merge(database);
		em.getTransaction().commit();
		return database;
	}

	@Override
	public void remove(Database database) {
		em.getTransaction().begin();
		Database mergedDatabase = em.merge(database);
		em.remove(mergedDatabase);			
		em.getTransaction().commit();			
	}

}
